﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //c1
            int[] numbers = { 12, 45, 67, 23, 9, 56, 32, 18, 5, 41 };

            // 使用LINQ计算
            int max = numbers.Max();
            int min = numbers.Min();
            double average = numbers.Average();
            int sum = numbers.Sum();

            Console.WriteLine($"数组: [{string.Join(", ", numbers)}]");
            Console.WriteLine($"最大值: {max}");
            Console.WriteLine($"最小值: {min}");
            Console.WriteLine($"平均值: {average:F2}");
            Console.WriteLine($"元素和: {sum}");
            Console.WriteLine($"按下任意键开始c3");
            Console.ReadLine();
            Random random = new Random();
            IShape[] shapes = new IShape[10];
            double totalArea = 0;

            Console.WriteLine("随机创建的10个形状:");
            for (int i = 0; i < 10; i++)
            {
                shapes[i] = ShapeFactory.CreateRandomShape(random);
                Console.WriteLine($"{i + 1}. {shapes[i]} - 面积: {shapes[i].CalculateArea():F2}");
                totalArea += shapes[i].CalculateArea();
            }

            Console.WriteLine($"\n所有形状的总面积: {totalArea:F2}");
            Console.ReadLine();
        }
    }
    // 简单工厂类
    public static class ShapeFactory
    {
        public static IShape CreateRandomShape(Random random)
        {
            int shapeType = random.Next(0, 3); // 0-长方形, 1-正方形, 2-三角形

            switch (shapeType)
            {
                case 0: // 长方形
                    return new Rectangle(
                        random.NextDouble() * 10 + 1,
                        random.NextDouble() * 10 + 1);

                case 1: // 正方形
                    return new Square(random.NextDouble() * 10 + 1);

                case 2: // 三角形
                        // 确保生成有效的三角形边长
                    double a, b, c;
                    do
                    {
                        a = random.NextDouble() * 10 + 1;
                        b = random.NextDouble() * 10 + 1;
                        c = random.NextDouble() * 10 + 1;
                    } while (!(a + b > c && a + c > b && b + c > a));

                    return new Triangle(a, b, c);

                default:
                    throw new InvalidOperationException("未知的形状类型");
            }
        }
    }
}
